async function getCurrentTab() {
  const [currentTab] = await chrome.tabs.query({
    active: true,
    lastFocusedWindow: true,
  });
  return currentTab;
}
// 整理书签
document.getElementById('toolkitBookmarks').addEventListener('click', () => {
  chrome.tabs.create({ url: "pages/bookmarksManager.html" });
});
// 使图片可点击
document
  .getElementById('toolkitImagesClickable')
  .addEventListener('click', async () => {
    const currentTab = await getCurrentTab();
    chrome.scripting.executeScript({
      target: { tabId: currentTab.id },
      func: () => {
        const imgArr = Array.from(document.getElementsByTagName('img'));
        imgArr.forEach(function (imgEle) {
          imgEle.classList.add('shy-clickable');
          imgEle.addEventListener('click', function (e) {
            window.open(e.target.getAttribute('src'), '_blank');
            console.log(e.target.getAttribute('src'));
            e.stopPropagation();
          });
        });
      },
    });
  });
// CSS Check
document
  .getElementById('toolkitCSSCheck')
  .addEventListener('click', async () => {
    const currentTab = await getCurrentTab();
    chrome.scripting.executeScript({
      target: { tabId: currentTab.id },
      func: () => {
        var elements = document.body.getElementsByTagName('*');
        var items = [];
        for (var i = 0; i < elements.length; i++) {
          if (
            elements[i].innerHTML.indexOf(
              'html * { outline: 1px solid red }'
            ) != -1
          ) {
            items.push(elements[i]);
          }
        }
        if (items.length > 0) {
          for (var i = 0; i < items.length; i++) {
            items[i].innerHTML = '';
          }
        } else {
          document.body.innerHTML +=
            '<style>html * { outline: 1px solid red }</style>';
        }
      },
    });
  });

// 将极客时间专栏文章内容保存为PDF
document
  .getElementById('savePDF')
  .addEventListener('click', async (event) => {
    const currentTab = await getCurrentTab();
    chrome.scripting.executeScript({
      target: { tabId: currentTab.id },
      files: ['contents/savePDF.js'], // 注入内容脚本
    });
    event.target.innerText = '下载中'
  });

document
  .getElementById('getWxreadCata')
  .addEventListener('click', async (event) => {
    const currentTab = await getCurrentTab();
    chrome.scripting.executeScript({
      target: { tabId: currentTab.id },
      files: ['contents/getWxreadCata.js'], // 注入内容脚本
    });
  });